package Ui.guiComponents;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Elya
 */
public class SetShips extends JPanel {
    static String clickedBoatName="";
    static String clickedBoatDirection="";
    private javax.swing.JToggleButton aircraftButton;
    private javax.swing.JToggleButton battleShipButton;
    private javax.swing.JToggleButton submarineButton;
    private javax.swing.JToggleButton cruiserButton;
    private javax.swing.JToggleButton destroyerButton;
    private javax.swing.JButton clearBoardButton;
    private javax.swing.JButton automaticButton;
    private javax.swing.JLabel clickLabel;
    private javax.swing.JLabel unclickLabel;
    private javax.swing.JComboBox comboChooseDirection;
    
    public SetShips(){
        init();
        //actionListeners();
        addListeners();
    }
    
    private void buttonAction(JToggleButton button){
        boolean boo=false;
        if (button.isSelected())
            boo=false;
        else
            boo=true;
        aircraftButton.setEnabled(boo);
        cruiserButton.setEnabled(boo);
        destroyerButton.setEnabled(boo);
        battleShipButton.setEnabled(boo);
        submarineButton.setEnabled(boo);
        if(!boo==true)
            button.setEnabled(!boo);
    
    }
    /*
    private void actionListeners(){

        aircraftButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAction(aircraftButton);
            }
        });
        
        cruiserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAction(cruiserButton);
            }
        });
        destroyerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAction(destroyerButton);
            }
        });
        battleShipButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAction(battleShipButton);
            }
        });
        submarineButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttonAction(submarineButton);
            }
        });
    }
*/
     
     public final void addListeners(){
         
         ItemListener listener = new ItemListener() {
         @Override
         public void itemStateChanged(ItemEvent e) {
                JToggleButton o = (JToggleButton) e.getSource();
                buttonAction((JToggleButton) e.getSource());
                String label = o.getToolTipText();
                if(o.isSelected()){
                    clickedBoatName=label;
                    comboChooseDirection.setSelectedIndex(0);
                    comboChooseDirection.setEnabled(true);

                }
                else{
                    comboChooseDirection.setSelectedIndex(-1);
                    comboChooseDirection.setEnabled(false);
                }

          }
        };
        ItemListener comboListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent itemEvent) {

                clickedBoatDirection= (String)comboChooseDirection.getSelectedItem();
                if (clickedBoatDirection==null)
                    clickedBoatDirection="";
                System.out.println(clickedBoatDirection);

          }
    };
        comboChooseDirection.addItemListener(comboListener);
        aircraftButton.addItemListener(listener);
        cruiserButton.addItemListener(listener);
        destroyerButton.addItemListener(listener);
        battleShipButton.addItemListener(listener);
        submarineButton.addItemListener(listener);
        

    }


    
    private void init(){
        
        
        aircraftButton = new javax.swing.JToggleButton();
        cruiserButton = new javax.swing.JToggleButton();
        destroyerButton = new javax.swing.JToggleButton();
        battleShipButton = new javax.swing.JToggleButton();
        submarineButton = new javax.swing.JToggleButton();
        clearBoardButton = new javax.swing.JButton();
        automaticButton = new javax.swing.JButton();
        clickLabel = new javax.swing.JLabel();
        unclickLabel = new javax.swing.JLabel();
        String[] comboItems = {"Horizontal", "Vertical"};
        comboChooseDirection = new javax.swing.JComboBox(comboItems);
        
        
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setBackground(new java.awt.Color(83, 138, 219));


        
        automaticButton.setBackground(new java.awt.Color(83, 138, 219));
        automaticButton.setText("Automatic Placement");
        automaticButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        automaticButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        

        clickLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); 
        clickLabel.setText("Click on a ship to place it");
        clickLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        clickLabel.setAlignmentY(Component.CENTER_ALIGNMENT);

        unclickLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        unclickLabel.setText("Then choose the it's direction");
        unclickLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        unclickLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        
        aircraftButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ui/guiComponents/images/carrierh.gif"))); 
        aircraftButton.setToolTipText("Aircraft Carrier");
        aircraftButton.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Aircraft Carrier", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
        aircraftButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        aircraftButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        battleShipButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ui/guiComponents/images/battleshiph.gif"))); 
        battleShipButton.setToolTipText("Battleship");
        battleShipButton.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Battleship", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
        battleShipButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        battleShipButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        submarineButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ui/guiComponents/images/submarineh.gif"))); 
        submarineButton.setToolTipText("Submarine");
        submarineButton.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Submarine", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
        submarineButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        submarineButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        cruiserButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ui/guiComponents/images/cruiserh.gif"))); 
        cruiserButton.setToolTipText("Cruiser");
        cruiserButton.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Cruiser", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP));
        cruiserButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        cruiserButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        destroyerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ui/guiComponents/images/destroyerh.gif"))); 
        destroyerButton.setToolTipText("Destroyer");
        destroyerButton.setBorder(javax.swing.BorderFactory.createTitledBorder("Destroyer"));
        destroyerButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        destroyerButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        clearBoardButton.setBackground(new java.awt.Color(83, 138, 219));
        clearBoardButton.setText("Clear Board");
        clearBoardButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        clearBoardButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        
        comboChooseDirection.setBackground(new java.awt.Color(83, 138, 219));
        //comboChooseDirection.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Horizontal", "Vertical" }));
        comboChooseDirection.setSelectedIndex(-1);
        comboChooseDirection.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ship Direction", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP));
        comboChooseDirection.setAlignmentX(Component.CENTER_ALIGNMENT);
        comboChooseDirection.setAlignmentY(Component.CENTER_ALIGNMENT);
        comboChooseDirection.setMaximumSize(new Dimension(86,45));
        comboChooseDirection.setEnabled(false);

       Dimension dimSize = new Dimension(220,10);
       Dimension dimSize2 = new Dimension(220,40);
        
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(automaticButton);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(clearBoardButton);
        add(new Box.Filler(dimSize2, dimSize2, dimSize2));
        add(clickLabel);
        add(unclickLabel);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(comboChooseDirection);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(aircraftButton);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(battleShipButton);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(submarineButton);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(cruiserButton);
        add(new Box.Filler(dimSize, dimSize, dimSize));
        add(destroyerButton);
       


        
    }


    
}
